//
//  WmcInviteMemberVC.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/14.
//

import UIKit

// MARK: - 扩展WmcTeamCloudInviteMemberVC + WmcNavigatorProtocol

extension WmcTeamCloudInviteMemberVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.inviewTeamMember]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vc = WmcTeamCloudInviteMemberVC()
        if let vm = parms.context as? WmcTeamCloudInviteMemberVM {
            vc = WmcTeamCloudInviteMemberVC(vm)
        }
        return vc
    }
}

// MARK: - 扩展WmcTeamCloudInviteMemberVC + WmcVMProtocol

extension WmcTeamCloudInviteMemberVC: WmcVMProtocol {
    typealias VMType = WmcTeamCloudInviteMemberVM
}

class WmcTeamCloudInviteMemberVC: WmcBaseVC {
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "邀请成员"

        /// 获取微信邀请相关信息
        viewModel.getQrcode()
    }

    override func makeUI() {
        makeNavView(showShadow: false)

        title = "邀请成员"

        view.backgroundColor = UIColor.wmc_gray_F7F8FA

        view.addSubview(noteView)
        view.addSubview(wechatInviteItemView)
        view.addSubview(qrCodeInviteItemView)
        view.addSubview(manualInviteItemView)

        noteView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom)
            make.left.right.equalToSuperview()
        }

        wechatInviteItemView.snp.makeConstraints { make in
            make.top.equalTo(noteView.snp.bottom)
            make.left.right.equalToSuperview()
        }
        qrCodeInviteItemView.snp.makeConstraints { make in
            make.top.equalTo(wechatInviteItemView.snp.bottom)
            make.left.right.equalToSuperview()
        }
        manualInviteItemView.snp.makeConstraints { make in
            make.top.equalTo(qrCodeInviteItemView.snp.bottom)
            make.left.right.equalToSuperview()
        }
    }

    override func bindViewModel() {
        noteView.teamId = viewModel.teamAblum?.teamOpenId

        /// 成员隐藏手动添加
        guard let memeber = viewModel.teamMember else { return }
        manualInviteItemView.isHidden = (memeber.role == .member)
    }

    override func bindViewAction() {
        noteView.copyBtn.rx.controlEvent(.touchUpInside).bind { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.copyTeamId(teamId: weakSelf.noteView.teamId)
        }.disposed(by: rx.disposeBag)

        wechatInviteItemView.rx.tapGesture().skip(1).bind { [weak self] _ in
            // 直接分享链接
            guard let weakSelf = self, let info = weakSelf.viewModel.inviationInfo.value else { return }

            WmcShareManager.shareLink(info.url, title: info.title, description: info.content, thumbImage: UIImage(named: "AppIcon"), scene: WmcShareManager.Scene.session) { _ in
            }
        }.disposed(by: rx.disposeBag)

        qrCodeInviteItemView.rx.tapGesture().skip(1).bind { [weak self] _ in
            guard let weakSelf = self, let team = weakSelf.viewModel.teamAblum else { return }
            WmcNavigatorManager.push(.teamInviteQRCodePage, parms: team)
        }.disposed(by: rx.disposeBag)

        manualInviteItemView.rx.tapGesture().skip(1).bind { [weak self] _ in
            guard let weakSelf = self, let team = weakSelf.viewModel.teamAblum else { return }
            WmcNavigatorManager.push(.manualInviteTeamMember, parms: team)
        }.disposed(by: rx.disposeBag)
    }

    lazy var noteView: WmcTeamInviteMemberNoteView = {
        let view = WmcTeamInviteMemberNoteView()
        return view
    }()

    lazy var wechatInviteItemView: WmcTeamMemberHeaderView = {
        let view = WmcTeamMemberHeaderView()
        view.model = ("we_chat_invite_icon", "微信邀请")
        return view
    }()

    lazy var qrCodeInviteItemView: WmcTeamMemberHeaderView = {
        let view = WmcTeamMemberHeaderView()
        view.model = ("qrCode_invite_icon", "二维码邀请")
        return view
    }()

    lazy var manualInviteItemView: WmcTeamMemberHeaderView = {
        let view = WmcTeamMemberHeaderView()
        view.model = ("manual_invite_icon", "手动添加")
        return view
    }()
}
